package com.fc.gateway.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fc.gateway.filter.PasswordDecoderFilter;
import com.fc.gateway.filter.FcRequestGlobalFilter;
import com.fc.gateway.filter.SwaggerBasicGatewayFilter;
import com.fc.gateway.filter.ValidateCodeGatewayFilter;
import com.fc.gateway.handler.GlobalExceptionHandler;
import com.fc.gateway.handler.ImageCodeHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Description: 网关配置，注入各种bean
 * @ClassName: GatewayConfiguration
 * @Author: weisn
 * @Date: 2022/12/16 11:37
 * @Version: 1.0
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(GatewayConfigProperties.class)
public class GatewayConfiguration {


    /**
     *  密码解密过滤器
     * @author weisn 
     * @param configProperties:
     * @see PasswordDecoderFilter
     * @date 2022/12/16 14:36
     */
    @Bean
    public PasswordDecoderFilter passwordDecoderFilter(GatewayConfigProperties configProperties) {
        return new PasswordDecoderFilter(configProperties);
    }


    /**
     *  gateway请求全局过滤器
     * @author weisn 
     * @see FcRequestGlobalFilter
     * @date 2022/12/16 14:45
     */
    @Bean
    public FcRequestGlobalFilter fcRequestGlobalFilter() {
        return new FcRequestGlobalFilter();
    }


    /**
     *  文档bean
     * @author weisn 
     * @param swaggerProperties:
     * @see SwaggerBasicGatewayFilter
     * @date 2022/12/16 14:45
     */
    @Bean
    @ConditionalOnProperty(name = "swagger.basic.enabled")
    public SwaggerBasicGatewayFilter swaggerBasicGatewayFilter(
            SpringDocConfiguration.SwaggerDocProperties swaggerProperties) {
        return new SwaggerBasicGatewayFilter(swaggerProperties);
    }

    /**
     *  校验验证码bean
     * @author weisn 
     * @param configProperties:
     * @param objectMapper:
     * @param redisTemplate: 
     * @see ValidateCodeGatewayFilter
     * @date 2022/12/16 15:25
     */
    @Bean
    public ValidateCodeGatewayFilter validateCodeGatewayFilter(GatewayConfigProperties configProperties,
                                                               ObjectMapper objectMapper, RedisTemplate redisTemplate) {
        return new ValidateCodeGatewayFilter(configProperties, objectMapper, redisTemplate);
    }

    @Bean
    public GlobalExceptionHandler globalExceptionHandler(ObjectMapper objectMapper) {
        return new GlobalExceptionHandler(objectMapper);
    }

    @Bean
    public ImageCodeHandler imageCodeHandler(RedisTemplate redisTemplate) {
        return new ImageCodeHandler(redisTemplate);
    }


}
